package com.devframe.srtp.server;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.devframe.srtp.Account;
import com.devframe.srtp.SRTPConstants;

/**
 * 
 * @author 詹保山
 */
public class SRTPServerContainer {
	
	private static Log log=LogFactory.getLog(SRTPServerContainer.class);

	/**
	 * 账户列表
	 */
	private List<Account> accounts=new ArrayList<Account>();
	
	/**
	 * SRTP文件存放根路径
	 */
	private  String srtpBaseDirectory;
	
	private  int port;
	
	private static SRTPServerContainer instance;
	
	
	public static SRTPServerContainer factoryMethod(){
		log.info("初始化静态资源传输平台");
		instance=new SRTPServerContainer();
		
		return instance;
	}
	
	public static SRTPServerContainer getInstance(){
		return instance;
	}
	
	
	/**
	 * 账户MAP结构
	 */
	private static  Map<String,Account> AccountMap=new HashMap<String,Account>();
	
	
	
	
	public void setAccounts(List<Account> accounts) {
		this.accounts = accounts;
		for(Account item:this.accounts){
			if(StringUtils.isBlank(item.getUserName()) || StringUtils.isBlank(item.getPwd())|| StringUtils.isBlank(item.getRootPath())){
				log.error("账户配置出现错误，请检查，"+item);
				new Exception().printStackTrace();
			}
			if(!SRTPServerContainer.AccountMap.containsKey(item.getUserName())){
				//重新定位路径
				SRTPServerContainer.AccountMap.put(item.getUserName(), item);
			}else{
				log.error("账户出现重复配置："+item);
			}
		}
		log.info("加入默认账户："+SRTPConstants.DEFAULT_ACCOUNT);
		SRTPServerContainer.AccountMap.put(SRTPConstants.DEFAULT_ACCOUNT.getUserName(), SRTPConstants.DEFAULT_ACCOUNT);
	}
	/**
	 * 获取账号
	 * @param userName
	 * @return
	 */
	public   Account getAccount(String userName){
		Account account=SRTPServerContainer.AccountMap.get(userName);
		if(account==null){
			log.error("不存在账户UserName="+userName+",返回默认账户");
			account=SRTPConstants.DEFAULT_ACCOUNT;
		}
		return account;
	}
 
	public   String getSRTPBaseDirectory() {
		return srtpBaseDirectory;
	}
	public void setSrtpBaseDirectory(String srtpBaseDirectory) {
		this.srtpBaseDirectory = srtpBaseDirectory;
	}
	
	public  int getPort() {
		return port;
	}
	
	public void setPort(int port) {
		this.port = port;
	}
 
	
 
	
	
	
	
	
}
